#######################################################
# Example of PingPong CDEVS 
# Author: moonho.hwang@gmail.com
#   Date: Oct.19.2011
#######################################################

from XSY.DEVS.CDEVS import *
from XSY.DEVS.CoupledDEVS import *
from XSY.DEVS.SimEngine import SimEngine

class PingPong(CDEVS):
   def __init__(self, name, parent=None):
      '''constructor'''
      init_tn=dict([("p1",1.), ("p2", float('inf'))])
      CDEVS.__init__(self, name, init_tn, parent)

   def delta_0(self):
      '''reset the initial state'''
      CDEVS.delta_0(self)
      self.C["p1"].tL = self.C["p2"].tL = 0 
      self.C["p1"].tN = float('inf')
      self.C["p2"].tN = 1 

   def delta_y(self):
      '''internal transition function'''
      self.C["p1"].tL = self.C["p2"].tL = Clock.tNow; 
      if self.C["p1"].tN == Clock.tNow:
         self.C["p1"].tN = float('inf')
         self.C["p2"].tN = Clock.tNow + 1 # player2 becomes active
      elif self.C["p2"].tN == Clock.tNow:
         self.C["p2"].tN = float('inf')
         self.C["p1"].tN = Clock.tNow + 1 # player1 becomes active
      else:
         raw_input("tnow=%f, %s=%f"%(Clock.tNow, self.IMMC, self.C[self.IMMC[0]].tN))

if __name__ == '__main__':
   pp = PingPong('PingPong')
   engine = SimEngine(pp)
   engine.runInteractiveMenu()
